<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.SubstoreDAO" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@page import="java.util.*"%>
<%@page import="javazoom.upload.*"%> 
<%@page import="uploadutilities.FileMover"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="jxl.Cell"%>
<%@page import="jxl.Sheet"%>
<%@page import="jxl.Workbook"%>
<%@page import="jxl.WorkbookSettings"%>
<%@page import="db.FororderDAO"%>
<%@page import="db.Fororder"%>
<%@page import="db.Forderpurmat"%>
<%@page import="db.Fororder"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Material"%>
<%@page import="db.ForderpurmatDAO"%>


<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%@ include file="/jsp/common/utility/ImportExecl.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	FororderDAO fororderDAO = (FororderDAO)ctx.getBean("FororderDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	ForderpurmatDAO forderpurmatDAO = (ForderpurmatDAO)ctx.getBean("ForderpurmatDAO");
	FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
	UploadBean upBean = new UploadBean();
	MultipartFormDataRequest mrequest = null;
	int tid = (Integer)session.getAttribute("tid");
	Hashtable files = null;
	boolean success = true;
	String message = "恭喜您，订单成功导入！";
	String serverPath = new String();
	serverPath = request.getParameter("serverPath");
	List listjxl = new ArrayList();
	if(serverPath!=null){
		listjxl = getListByJxl(serverPath,success,message);
		request.setAttribute("serverPath",serverPath);
	}
	List listmap = new ArrayList();
	
	if(listjxl!=null && listjxl.size()>0){
		for(int i=0;i<listjxl.size();i++){
			List li = (List)listjxl.get(i);
			
			
			Fororder fororder = new Fororder();
			if(li!=null && li.size()>14){
				//订单id初始
				int fororderId = 0;
				List forlist = fororderDAO.findByOrderCode(li.get(4));
				if(forlist!=null && forlist.size()>0){
					Fororder forder = (Fororder)forlist.get(0);
					fororderId = forder.getId();
					////////*************订单号已经存在直接解析物资。即可*****************///////////
					Forderpurmat forderpurmat = new Forderpurmat();
							List matlist = materialDAO.findByCode(li.get(10));
							if(matlist!=null && matlist.size()>0){//找到物资
								for(int m=0;m<matlist.size();m++){
									Material material = (Material)matlist.get(m);
									if(material.getIsDelete()==0 && material.getTeamId()==tid){
										forderpurmat.setMaterialId(material.getId());
										forderpurmat.setMaterialCode(material.getCode());
										forderpurmat.setMaterialName(material.getName());
									}else{
										success = false;
										message = "物资编码在系统中不存在。";
										request.setAttribute("success",success);
										request.setAttribute("message",message);
										pageContext.forward("/vm/procure/procureImportExecl.vm");
										return;
									}
								}
								
							}else{
								
								success = false;
								message = "物资编码在系统中不存在。";
								request.setAttribute("success",success);
								request.setAttribute("message",message);
								pageContext.forward("/vm/procure/procureImportExecl.vm");
								return;
							}
							if(li.get(13)!=null){
								forderpurmat.setOrderNum(Integer.parseInt((String)li.get(13)));
							}else{
								forderpurmat.setOrderNum(0);
							}
							
							if(li.get(14)!=null){
								forderpurmat.setOrderPrice(Float.parseFloat((String)li.get(14)));
							}else{
								forderpurmat.setOrderPrice(Float.parseFloat("0"));
							}
							List lpro = projectDAO.findByCode(li.get(2));
							if(li.get(2)!=null && (li.get(2)!="" || !"".equals(li.get(2)))){
								if(lpro!=null && lpro.size()>0){
									for(int lp=0;lp<lpro.size();lp++){
										Project project = (Project)lpro.get(lp);
										if(project.getIsDelete()==0 && project.getTeamId()==tid){//有效
											forderpurmat.setProCode(project.getCode());
											forderpurmat.setProName(project.getName());
											forderpurmat.setProId(project.getId());
										}else{
											success = false;
											message = "项目信息在系统中不存在。";
											request.setAttribute("success",success);
											request.setAttribute("message",message);
											pageContext.forward("/vm/procure/procureImportExecl.vm");
											return;
										}
									}
									
								}else{
										success = false;
										message = "项目信息在系统中不存在。";
										request.setAttribute("success",success);
										request.setAttribute("message",message);
										pageContext.forward("/vm/procure/procureImportExecl.vm");
										return;
								}
							}else{
								forderpurmat.setProCode("");
								forderpurmat.setProName("");
								forderpurmat.setProId(0);
							}
							forderpurmat.setForOrderId(fororderId);
							forderpurmatDAO.save(forderpurmat);
					
					
					
					////////******************************//////////
					
				}else{
					if(li.get(4)!=null){
						fororder.setOrderCode((String)li.get(4));
					}else{
						fororder.setOrderCode("");
					}
					
					//////子库
					if(li.get(1)=="通用库" || "通用库".equals(li.get(1)) || li.get(1)=="专项库" || "专项库".equals(li.get(1))){
						if(li.get(1)=="通用库" || "通用库".equals(li.get(1))){
							fororder.setSubstoreId(2);
						}
						if(li.get(1)=="专项库" || "专项库".equals(li.get(1))){
							fororder.setSubstoreId(1);
						}
					}else{
						success = false;
						message = "子库信息错误。";
						request.setAttribute("success",success);
						request.setAttribute("message",message);
						pageContext.forward("/vm/procure/procureImportExecl.vm");
						return;
					}
					
					//项目编码
				
					List listpro = projectDAO.findByCode(li.get(2));
					if(li.get(2)!=null && (li.get(2)!="" || !"".equals(li.get(2)))){
						if(listpro!=null && listpro.size()>0){
							for(int p=0;p<listpro.size();p++){
								Project project = (Project)listpro.get(p);
								if(project.getIsDelete()==0 && project.getTeamId()==tid){//有效
									fororder.setProCode(project.getCode());
									fororder.setProName(project.getName());
									fororder.setProId(project.getId());
								}else{
									success = false;
									message = "项目信息在系统中不存在。";
									request.setAttribute("success",success);
									request.setAttribute("message",message);
									pageContext.forward("/vm/procure/procureImportExecl.vm");
									return;
								}
							}
							
						}else{
								success = false;
								message = "项目信息在系统中不存在。";
								request.setAttribute("success",success);
								request.setAttribute("message",message);
								pageContext.forward("/vm/procure/procureImportExecl.vm");
								return;
						}
					}else{
						fororder.setProCode("");
						fororder.setProName("");
						fororder.setProId(0);
					}
					
					if(li.get(5)!=null){
						fororder.setOrderDate((String)li.get(5));
					}else{
						fororder.setOrderDate("");
					}
					
					
					if(li.get(6)!=null){
						fororder.setOrderState((String)li.get(6));
					}else{
						fororder.setOrderState("");
					}
					
					if(li.get(7)!=null){
						fororder.setBuyUser((String)li.get(7));
					}else{
						fororder.setBuyUser("");
					}
					
					if(li.get(8)!=null){
						fororder.setOrderRemark((String)li.get(8));
					}else{
						fororder.setOrderRemark("");
					}
					
					if(li.get(9)!=null){
						fororder.setSupplier((String)li.get(9));
					}else{
						fororder.setSupplier("");
					}
					
					if(li.get(15)!=null){
						fororder.setOrderMoney(Float.parseFloat((String)li.get(15)));
					}else{
						fororder.setOrderMoney(Float.parseFloat("0"));
					}
					fororder.setIsConvert(0);//未转
					fororder.setTeamId(tid);
					fororder.setOrderType(0);//0正式。1临时
					fororder.setDate(new Date());
					
					fororderDAO.save(fororder);
					if(fororder.getId()!=0){//物资存入成功
							fororderId = fororder.getId();
							Forderpurmat forderpurmat = new Forderpurmat();
							List matlist = materialDAO.findByCode(li.get(10));
							if(matlist!=null && matlist.size()>0){//找到物资
								for(int m=0;m<matlist.size();m++){
									Material material = (Material)matlist.get(m);
									if(material.getIsDelete()==0 && material.getTeamId()==tid){
										forderpurmat.setMaterialId(material.getId());
										forderpurmat.setMaterialCode(material.getCode());
										forderpurmat.setMaterialName(material.getName());
									}else{
										success = false;
										message = "物资编码在系统中不存在。";
										request.setAttribute("success",success);
										request.setAttribute("message",message);
										pageContext.forward("/vm/procure/procureImportExecl.vm");
										return;
									}
								}
								
							}else{
								
								success = false;
								message = "物资编码在系统中不存在。";
								request.setAttribute("success",success);
								request.setAttribute("message",message);
								pageContext.forward("/vm/procure/procureImportExecl.vm");
								return;
							}
							if(li.get(13)!=null){
								forderpurmat.setOrderNum(Integer.parseInt((String)li.get(13)));
							}else{
								forderpurmat.setOrderNum(0);
							}
							
							if(li.get(14)!=null){
								forderpurmat.setOrderPrice(Float.parseFloat((String)li.get(14)));
							}else{
								forderpurmat.setOrderPrice(Float.parseFloat("0"));
							}
							List lpro = projectDAO.findByCode(li.get(2));
							if(li.get(2)!=null && (li.get(2)!="" || !"".equals(li.get(2)))){
								if(lpro!=null && lpro.size()>0){
									for(int lp=0;lp<lpro.size();lp++){
										Project project = (Project)lpro.get(lp);
										if(project.getIsDelete()==0 && project.getTeamId()==tid){//有效
											forderpurmat.setProCode(project.getCode());
											forderpurmat.setProName(project.getName());
											forderpurmat.setProId(project.getId());
										}else{
											success = false;
											message = "项目信息在系统中不存在。";
											request.setAttribute("success",success);
											request.setAttribute("message",message);
											pageContext.forward("/vm/procure/procureImportExecl.vm");
											return;
										}
									}
									
								}else{
										success = false;
										message = "项目信息在系统中不存在。";
										request.setAttribute("success",success);
										request.setAttribute("message",message);
										pageContext.forward("/vm/procure/procureImportExecl.vm");
										return;
								}
							}else{
								forderpurmat.setProCode("");
								forderpurmat.setProName("");
								forderpurmat.setProId(0);
							}
							forderpurmat.setForOrderId(fororderId);
							forderpurmatDAO.save(forderpurmat);
					}else{
						success = false;
						message = "订单导入出错。";
						request.setAttribute("success",success);
						request.setAttribute("message",message);
						pageContext.forward("/vm/procure/procureImportExecl.vm");
						return;
					}
				}
				
			}else{
				success = false;
				message = "解析出错，execl文件格式行数不够，请仔细检查execl文件。";
				request.setAttribute("success",success);
				request.setAttribute("message",message);
				pageContext.forward("/vm/procure/procureImportExecl.vm");
				return;
			}
			
			
		}
	}
	request.setAttribute("listjxl",listmap);
	request.setAttribute("success",success);
	request.setAttribute("message",message);
	pageContext.forward("/vm/procure/procureImportExecl.vm");
%>